#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int t, n;
string s;

vector<string> vs = { "aa", "aba", "aca", "abca", "acba", "abbacca", "accabba" };

int main() {
    cin >> t;
    while (t--) {
        cin >> n >> s;
        int res = -1;
        for (auto str : vs) {
            if (s.find(str) != string::npos) {
                res = str.size();
                break;
            }
        }
        cout << res << endl;
    }
    return 0;
}